/*
 * Created on 10/06/2006
 *
 * Colecciones : TreeMap implements SortedMap 
 *
 */

package _04_Map;

import java.util.*;

public class TreeMapDemo {
	SortedMap<String,String> treeMap;
	
	public TreeMapDemo() {
		treeMap = new TreeMap<String,String>(); 	
		agregar();
		buscar();
		eliminar();
		posicionar();
		rangos();		
		vaciar();
	}
	
	// put(Object key, Object val)
	void agregar() {
		treeMap.put("aa", "AAAA");
		treeMap.put("hh", "HHHH");
		treeMap.put("vv", "VVVV");
		treeMap.put("ss", "SSSS");
		treeMap.put("ee", "EEEE");
		treeMap.put("oo", "OOOO");
		treeMap.put("bb", "BBBB");
		treeMap.put("pp", "PPPP");
		mostrar();
	}
	
	// containsValue(Object val)	containsKey(Object key)
	void buscar() {
		System.out.print("\ncontainsValue(EEEE): " + treeMap.containsValue("EEEE"));
		System.out.print("\tcontainsKey(pp): " + treeMap.containsKey("pp"));
		System.out.println();
	}	
	
	// remove(Object key)
	void eliminar() {
		System.out.print("\nremove(ee): " + treeMap.remove("ee"));
		System.out.println();
		mostrar();
	}
	
	// firstKey() lastKey()
	void posicionar() {
		System.out.print("\nfirst(): " + treeMap.firstKey());
		System.out.println("\tlast(): " + treeMap.lastKey());
	}	
	
	//	headMap(...) tailMap(...) subMap(...)
	void rangos() {
		// headMap
		SortedMap <String,String> sMap = treeMap.headMap("ll");
		Set <String> set = sMap.keySet();
		System.out.print("\nheadMap(ll): ");
		for (Iterator <String> i = set.iterator(); i.hasNext();) {
			System.out.print(i.next()+"->");
		}
		System.out.println();
		// tailMap
		sMap = treeMap.tailMap("ll");
		set = sMap.keySet();
		System.out.print("tailMap(ll): ");
		for (Iterator <String>i = set.iterator(); i.hasNext();) {
			System.out.print(i.next()+"->");
		}
		System.out.println();	
		// subMap
		sMap = treeMap.subMap("gg", "rr");
		set = sMap.keySet();
		System.out.print("subMap(gg, rr): ");
		for (Iterator<String> i = set.iterator(); i.hasNext();) {
			System.out.print(i.next()+"->");
		}
		System.out.println();			
	}	
	
	// clear() isEmpty()
	void vaciar() {
		treeMap.clear();
		System.out.println("\nclear() ... lista vacia ? " + treeMap.isEmpty());
	}	
	
	// get(Object key) keySet() 
	void mostrar() {
		Set<String> set = treeMap.keySet();
		System.out.print("claves : ");
		for (Iterator <String>i = set.iterator(); i.hasNext();) {
			System.out.print(i.next()+"->");
		}
		
		System.out.print("\nvalores: ");
		for (Iterator <String>i = set.iterator(); i.hasNext();) {
			System.out.print(treeMap.get(i.next())+"->");
		}	
		System.out.println();
	}
	
	public static void main(String[] args){
		new TreeMapDemo();
		System.exit(0);
	}
}
